package arcomage;

import java.util.*; // for Lists

/** This is an abstract class for an Arcomage game, which performs underlying
 *  game mechanics. Animation and notification must be handled by a subclass.
 *  @author Joshua Barnes
 *  @version 13th December 2009
 */
public abstract class AmGame
{
    private String tavernName;
    private AmDeck deck;
    private List<AmPlayer> players;
    private AmRules rules;

    public AmGame(String tavernNameIn, List<AmPlayer> playersIn, AmRules rulesIn)
    {
        refreshDeck();
        setTavernName(tavernNameIn);
        setRules(rulesIn);
        populatePlayerList(playersIn);
    }
    /** fills the game's list of players. Play proceeds in list order.*/
    public void populatePlayerList(List<AmPlayer> playersIn)
    {
        players = playersIn;
    }
    /** Sets a new set of rules for the game. */
    public void setRules(AmRules rulesIn)
    {
        rules = rulesIn;
    }
    /** Sets the name of the tavern for the current game */
    public void setTavernName(String tavernNameIn)
    {
        tavernName = tavernNameIn;
    }
    /** Initialises a new Game, with the specified rule set */
    public void setUpGame()
    {
        // deal initial cards out
        for (AmPlayer currentPlayer: players)
        {
            int numToDraw = AmPlayer.maxHandSize - currentPlayer.handSize();
            currentPlayer.pickUpCards(deck.drawCards(numToDraw));
        }
    }
    /** Refreshes the deck to a new, full, shuffled, arcomage deck.*/
    public void refreshDeck()
    {
        deck = new AmDeck();
    }
    /** Gets the appropriate player to play a round */
    public void playRound(int currentPlayerIndexIn)
    {
        //TODO
    }
    /** Returns a boolean value denoting whether any player has won the game yet*/
    public boolean hasWon(int playerIndex)
    {
        AmPlayer currentPlayer = players.get(playerIndex);
        if (currentPlayer.getBeasts() >= rules.winningResourceValue
         || currentPlayer.getGems()   >= rules.winningResourceValue
         || currentPlayer.getQuarry() >= rules.winningResourceValue
         || currentPlayer.getTower()  >= rules.winningTower)
        {
            return true;
        }
        else
        {
            // check whether all the other players have lost
            for (int ii=0; ii<players.size(); ii++)
            {
                if ( ii != playerIndex                 // other player
                  && players.get(ii).getTower()>0)   // still has tower
                {
                    return false;
                }
            }
            // all other players are out of action
            return true;
        }
    }
    /** Returns the index of the winning player, or -1 if no player has won yet*/
    public int winningPlayer()
    {
        for (int i = 0; i < players.size(); i++)
        {
            if (hasWon(i))
            {
                return i;
            }

        }
        return -1;
    }
    /** Returns a boolean value denoting whether the game has finished yet */
    public boolean gameFinished()
    {
        if (winningPlayer() == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
